//
//  MineViewController.m
//  Cars' home
//
//  Created by dapeng on 15/11/27.
//  Copyright © 2015年 dapeng. All rights reserved.
//

#import "MineViewController.h"
#import "Theme-PrefixHeader.pch"
#import "SAPCollectViewController.h"
#import "SAPQuestionViewController.h"
@interface MineViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UIImageView *vvv;
@end

@implementation MineViewController
- (void)dealloc {
    [_myTableView release];
    [_switchButton release];
    [_myView release];

    [super dealloc];
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self tableViews];
    self.navigationItem.title = @"我的";

     self.switchButton = [[UISwitch alloc] initWithFrame:CGRectMake(screenWidth-20-42, 15, 42, 22)];
//    [self night];
}
- (void)night {
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord = [user objectForKey:@"nightModel"];
    
    if (!(passWord == nil) && [passWord isEqualToString:@"day"]) {
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithWhite:0.126 alpha:1.000];
        
    }else{
        self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
        self.tabBarController.tabBar.barTintColor = [UIColor whiteColor];
        
    }
    
}

- (void)tableViews {
    self.view.backgroundColor = [UIColor whiteColor];
    _dataArray1 = [[NSMutableArray alloc]initWithObjects:@"常见问题",@"我的电台",@"夜间模式",nil];
    _dataArray2 = [[NSMutableArray alloc]initWithObjects:@"我的收藏",@"清理缓存",nil];
    _dataArray3 = [[NSMutableArray alloc]initWithObjects:@"关于我们",nil];
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, screenWidth, screenHeight - 44) style:UITableViewStylePlain];
    _myTableView.tableFooterView = [[UIView alloc]init];
    _myTableView.dataSource = self;
    _myTableView.delegate = self;
    _myTableView.showsHorizontalScrollIndicator = NO;
    _myTableView.backgroundColor = [UIColor clearColor];
    _myTableView.backgroundView = nil;
    _myTableView.backgroundColor = [[ConfigurationTheme shareInstance] getThemeColorWithName:@"tgColor"];
    [self.view addSubview:_myTableView];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGFLOAT_MIN;
    }else {
    return 20;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            SAPQuestionViewController *questionVC = [[SAPQuestionViewController alloc] init];
            [self.navigationController pushViewController:questionVC animated:NO];
        }if (indexPath.row == 1) {
            SAPAudioViewController *audioVC = [[SAPAudioViewController alloc] init];
            [self.navigationController pushViewController:audioVC animated:YES];
        }
        
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 1) {
            NSInteger size = [[SDImageCache sharedImageCache] getSize];
        
            float totalSize = size / 1024.0 / 1024.0;
            
            // NSString *clearMessage = tmpSize >= 1024 * 1024 ? [NSString stringWithFormat:@"清理缓存(%.2fM)" , tmpSize / 1024 / 1024] : [NSString stringWithFormat:@"清理缓存(%.2fK)", tmpSize / 1024];
            
            UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"清除了%.2fMb", totalSize] preferredStyle:1];
            
            
            
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:0 handler:^(UIAlertAction * _Nonnull action) {
            
            [[SDImageCache sharedImageCache] clearDisk];
            
            //清除内存缓存
            
            [[[SDWebImageManager sharedManager] imageCache] clearMemory];
            
            //清除系统缓存
            
            [[NSURLCache sharedURLCache] removeAllCachedResponses];
            
//            [[BaseData shardBaseData]dropTable];
            
        }];
            [alter addAction:action];
            [self presentViewController:alter animated:YES completion:^{
        }];
    }
}if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            SAPCollectViewController *collectVC = [[SAPCollectViewController alloc] init];
        [self.navigationController pushViewController:collectVC animated:NO];
        }
    }if (indexPath.section == 2) {
        SAPAboutCarViewController *aboutVC = [[SAPAboutCarViewController alloc] init];
        [self.navigationController pushViewController:aboutVC animated:NO];
        
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    _myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 20)];
    _myView.backgroundColor = [[ConfigurationTheme shareInstance] getThemeColorWithName:@"tgColor"];
    return _myView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    switch (section) {
        case 0:
            return  _dataArray1.count;
            break;
        case 1:
            return  _dataArray2.count;
            break;
        case 2:
            return  _dataArray3.count;
            break;
        default:
            return 0;
            break;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
#pragma mark - tableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString*cellId=@"cellId";
    UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:cellId];
    if(!cell){
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        }
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord = [user objectForKey:@"nightModel"];
    
    if (!(passWord == nil) && [passWord isEqualToString:@"day"]){
        
        
        cell.contentView.backgroundColor = [UIColor colorWithWhite:0.126 alpha:1.000];
        cell.backgroundColor = [UIColor colorWithWhite:0.126 alpha:1.000];
        cell.textLabel.textColor = RGB(153.0,153.0,153.0,1.0f);
        self.view.backgroundColor = [UIColor colorWithWhite:0.126 alpha:1.000];
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithWhite:0.126 alpha:1.000];
        
    }else{
        cell.backgroundColor = [UIColor whiteColor];
        cell.contentView.backgroundColor = [UIColor clearColor];
        cell.textLabel.textColor = [UIColor blackColor];
        self.view.backgroundColor = [UIColor whiteColor];
        self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    }
    if (indexPath.section==0) {
        cell.textLabel.text=[_dataArray1 objectAtIndex:indexPath.row];
        
        if (indexPath.row == 0){
            cell.imageView.image = [UIImage imageNamed:@"questionIcon"];
        }else if(indexPath.row == 1){
            cell.imageView.image = [UIImage imageNamed:@"audioIcon"];
        }else{
            
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            NSString *passWord = [ user objectForKey:@"nightModel"];
            if (!(passWord == nil) && [passWord isEqualToString:@"night"]){
                [_switchButton setOn:NO];
            }else{
                [_switchButton setOn:YES];
                _switchButton.onTintColor = RGB(4.0,80.0,85.0,0.9f);
                _switchButton.thumbTintColor = RGB(4.0,43.0,85.0,1.0f);
            }
            
            [_switchButton addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
            self.switchButton.thumbTintColor = [UIColor colorWithWhite:0.929 alpha:1.000];//按钮颜色
            self.switchButton.onTintColor = [UIColor colorWithWhite:0.617 alpha:1.000];//开后的颜色
            self.switchButton.tintColor = [UIColor colorWithWhite:0.876 alpha:1.000];//线颜色
            self.switchButton.layer.borderColor = [UIColor colorWithWhite:0.747 alpha:1.000].CGColor;
            [cell.contentView addSubview:_switchButton];
            cell.imageView.image = [UIImage imageNamed:@"yejian"];
            [cell.contentView addSubview:_switchButton];
            
        }
            cell.selectionStyle = NO;
            cell.accessoryType = NO;
    }
    if (indexPath.section==1) {
        cell.textLabel.text=[_dataArray2 objectAtIndex:indexPath.row];
        UIImageView*arrowImageView=[[UIImageView alloc]init];;
        arrowImageView.frame=CGRectMake(screenWidth-13-11, (60-20)/2, 10, 18);
        arrowImageView.image=[UIImage imageNamed:@"arrow.png"];
        [cell.contentView addSubview:arrowImageView];
        if (indexPath.row == 0) {
            cell.imageView.image = [UIImage imageNamed:@"collectIcon"];
        }else {
            cell.imageView.image = [UIImage imageNamed:@"clearIcon"];
        }
    }
    if (indexPath.section==2) {
        cell.textLabel.text=[_dataArray3 objectAtIndex:indexPath.row];
        UIImageView*arrowImageView=[[UIImageView alloc]init];;
        arrowImageView.frame=CGRectMake(screenWidth-13-11, (60-20)/2, 10, 18);
        arrowImageView.image=[UIImage imageNamed:@"arrow.png"];
        [cell.contentView addSubview:arrowImageView];
        cell.imageView.image = [UIImage imageNamed:@"aboutIcon"];
    }
    cell.selectionStyle = NO;
    cell.accessoryType = NO;
    return cell;
}
#pragma mark ---- nightMode
-(void)switchAction:(id)sender{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord = [user objectForKey:@"nightModel"];
    if ([passWord isEqualToString:@"night"]){
        NSString *passWord = @"day";
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        [user setObject:passWord forKey:@"nightModel"];

    
    }else{
        NSString *passWord = @"night";
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        [user setObject:passWord forKey:@"nightModel"];

    }
    
    [_myTableView reloadData];
}- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated {
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord = [ user objectForKey:@"nightModel"];
    if (!(passWord == nil) && [passWord isEqualToString:@"night"]){
        [_switchButton setOn:NO];
        
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
